import recursion from '@/util/recursion';
export default {
	data() {
		return {
			orgList: [],
			userArr:[],
			roleArr:[],
			disable:'',
		};
	},
	mounted() {
		this.getOrgList();
	},
	methods: {
		getOrgList() {
			this.axios({
				method: 'get',
				url: '/basic/organization/findListByToken',
				params: { level: 1 },
			}).then((res) => {
				if (res.code === 200) {
					const obj = {
						label: 'name',
						value: 'code',
						children: 'children',
					};
					this.orgList = recursion.convertTree(res.data, obj);
				}
			});
		},
		//角色
		searchRole() {
			this.axios({
				method: 'get',
				url: '/basic/role/page',
				params: {
					pageNum: 1,
					pageSize: 100,
				},
			}).then((res) => {
				if (res.code === 200) {
					this.roleArr = res.data.list;
				} else {
					this.$Message.warning({ content: res.msg, duration: 6, closable: true });
				}
			});
		},
		searchUser() {
			this.axios({
				method: 'get',
				url: '/basic/user/searchUser',
			}).then((res) => {
				if (res.code === 200) {
					this.userArr = res.data;
				} else {
					this.$Message.warning({ content: res.msg, duration: 6, closable: true });
				}
			});
		},
		userChange(user) {
			if (user && user.length > 0) {
				this.$set(this.formItem, 'multipleUser', user);
				this.disable = 'roleDisable';
				this.$set(this.formItem, 'multipleRoles', []);
			} else this.disable = '';
		},
		roleChange(roleId) {
			if (roleId && roleId.length > 0) {
				this.$set(this.formItem, 'multipleRoles', roleId);
				this.disable = 'userDisable';
				this.$set(this.formItem, 'multipleUser', []);
			} else this.disable = '';
		},
		roleAndUserPass(rule, value, callback) {
			const { multipleRoles, multipleUser } = this.formItem;
			const validatePass = () => {
				if (rule.field == 'multipleRoles' && multipleRoles.length == 0) {
					callback(new Error('请选择角色'));
				} else if (rule.field == 'multipleUser' && multipleUser.length == 0) {
					callback(new Error('请选择用户'));
				}
			};
			if (multipleRoles && multipleRoles.length > 0 && multipleUser && multipleUser.length > 0) {
				validatePass();
			} else if (!multipleRoles && !multipleUser) {
				callback(new Error('请选择用户或者角色'));
			}
			callback();
		},
		timeRemindPass(rule, value, callback) {
			if (this.formItem.templateType == 1) {
				callback();
			} else if (value == '') {
				callback(new Error('请选择时间'));
			}
		},
		addOk(name,status) {
			this.$refs[name].validate((valid) => {
				if (valid) {
					if (this.formItem.id) {
						this.axios({
							method: 'post',
							url: '/dms/messageWarning/updateMessageWarningPlan',
							data: {
								...this.formItem,
								notificationMethod: status,
							},
						}).then((res) => {
							if (res.code === 200) {
								this.$emit('confirm');
								this.createStatus = false;
								this.$Message.success(res.msg);
							} else {
								this.$Message.warning({ content: res.msg, duration: 6, closable: true });
							}
						});
					} else {
						this.axios({
							method: 'post',
							url: '/dms/messageWarning/addMessageWarningPlan',
							data: {
								...this.formItem,
								notificationMethod: status,
							},
						}).then((res) => {
							if (res.code === 200) {
								this.$emit('confirm');
								this.createStatus = false;
								this.$Message.success(res.msg);
							} else {
								this.$Message.warning({ content: res.msg, duration: 6, closable: true });
							}
						});
					}
				}
			});
		},
		keyUp(e, key, money) {
			// 如果是删除为空 则不处理
			if (!this.formItem[key]) {
				return (this.formItem[key] = '');
			}
			// 每次抬键处理,对应金额渲染两位小数||数字
			let num = '';
			if (money) {
				num = this.formItem[key].match(/^\d*(\.?\d{0,2})/g)[0];
			} else {
				num = this.formItem[key].replace(/^[^\d]+$/g, '').split('.')[0];
			}
			this.formItem[key] = `${num}`;
		},
	},
};